package tech.aistar.day03.cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(urlPatterns = "/cookie/create")
public class CookieCreateController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = "星星";
        //一级编码
        name = URLEncoder.encode(name,"utf-8");
        //二级编码
        name = URLEncoder.encode(name,"utf-8");
        //创建一个Cookie对象
        Cookie c = new Cookie("username","星星");
        //设置域
        c.setPath("/");
        //设置过期时间 - s
        c.setMaxAge(60*10);//十分钟

        //通过响应头信息将该cookie发送到client
        resp.addCookie(c);
    }
}
